package com.dazzle.system.controller;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dazzle.core.util.query.QueryFilter;
import com.dazzle.system.entity.SysType;
import com.dazzle.system.service.SysTypeService;

import flexjson.JSONSerializer;

@Controller
@RequestMapping("/system")
public class SysTypeController{
	@Resource
	private SysTypeService sysTypeService;
	
	@RequestMapping("/typelist")
	public String list(HttpServletRequest request,HttpServletResponse response){
//		QueryFilter queryFilter = new QueryFilter(request);
//		List<SysType> list = sysTypeService.getAll(queryFilter);
////		request.setAttribute("typeList", list);
//		JSONSerializer json = new JSONSerializer();
//		request.setAttribute("typeList", json.serialize(list));
//		request.setAttribute("pageBean", queryFilter.getPagingBean());
		
		List<SysType> typeRoots = sysTypeService.getAllRootOfSys();
		request.setAttribute("typeRoots", typeRoots);
		return "system/sys_type_list";
	}
	
	@RequestMapping("/json_get_type_list")
	public String jsonList(HttpServletRequest request,HttpServletResponse response){
		QueryFilter queryFilter = new QueryFilter(request);
		List<SysType> list = sysTypeService.getAll(queryFilter);
//		request.setAttribute("typeList", list);
		JSONSerializer json = new JSONSerializer();
		request.setAttribute("jsonString", json.serialize(list));
		return "jsonString";
	}
	@RequestMapping("/json_get_type_tree")
	public String tree(HttpServletRequest request,HttpServletResponse response){
		StringBuffer tree = new StringBuffer("");
		String rootId = request.getParameter("rootId");
		SysType root = new SysType();
		if(StringUtils.isEmpty(rootId)){
			rootId = "0";
			root.setTypeId(0l);
			root.setTypeName("总分类");
		}else{
			root = sysTypeService.get(new Long(rootId));
		}
		tree.append("{'text':'").append(root.getTypeName())
		.append("','id':").append(root.getTypeId());
		appendChrildren(tree,new Long(rootId));
		tree.append("}");
		
		request.setAttribute("jsonString", tree);
		return "jsonString";
	}
	
	private void appendChrildren(StringBuffer json, Long rootId){
		List<SysType> children = sysTypeService.getAllByParentId(rootId);
		if(children.size()>0){
			json.append(",'expanded': true,'classes':'folder','children':[");
			for(SysType child :children){
				json.append("{'text':'").append(child.getTypeName()).append("','id:':'").append(child.getTypeId()).append("'");
				appendChrildren(json,child.getTypeId());
				json.append("},");
			}
			json.deleteCharAt(json.length()-1);
			json.append("]");
		}else{
			json.append(",'classes':'file'");
		}
	}
	
	@RequestMapping("/add_sys_type")
	public String add(@ModelAttribute("SysTypeForm") SysType sysType,HttpServletRequest request) {
		if(sysType!=null){
//			HashSet<SysResource> resources = new HashSet<SysResource>();
//			String[] resIds = request.getParameterValues("resIds");
//			if(resIds !=null && resIds.length>0){
//				for(String resId:resIds){
//					if(StringUtils.isNotEmpty(resId))
//						resources.add(sysResourceService.get(new Long(resId)));
//				}
//			}
//			sysType.setResources(resources);
			sysTypeService.save(sysType);
		}
		return "forward:typelist.do";
	}
	@RequestMapping("/json_get_sys_type")
	public  String get(HttpServletRequest request,HttpServletResponse response) {
//		String typeId = request.getParameter("editId");
//		if(StringUtils.isNotEmpty(typeId)){
//			SysType sysType = sysTypeService.get(new Long(typeId));
//			JSONSerializer json = new JSONSerializer();
//			request.setAttribute("jsonString", json.include("resources").serialize(sysType));
//		}
		return "jsonString";
	}
	
	@RequestMapping("/json_del_sys_type")
	public String del(HttpServletRequest request,HttpServletResponse response){
//		String ids = request.getParameter("ids");
//		StringBuffer msg = new StringBuffer("{'success':'true'");
//		if(StringUtils.isNotEmpty(ids)){
//			String[] typeIds = ids.split(",");
//			msg.append(",'msg':'Role ");
////			for(String typeId : typeIds){
////				if(StringUtils.isNotEmpty(typeId)){
////					SysType readyToRemove = SysTypeService.get(new Long(typeId));
////					msg.append(readyToRemove.getRoleName()).append(",");
////					SysTypeService.remove(readyToRemove);
////				}
////			}
//			msg.deleteCharAt(msg.length()-1);
//			msg.append(" had been remove!'");
//		}
//		msg.append("}");
//		request.setAttribute("jsonString", msg);
		return "jsonString";
	}
}